/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.database.tables;

import aiaudio.database.Family;
import aiaudio.database.qualifiers.IntQualifier;
import aiaudio.database.qualifiers.LongQualifier;
import aiaudio.database.qualifiers.StringQualifier;
import java.io.IOException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

/**
 *
 * @author nastya
 */
public class UsersListTable extends TableStructure {

    public static final Family DATA = new Family("d");
    private static final Family STATISTICS = new Family("s");
    private static final LongQualifier VOTE_COUNT = new LongQualifier("votes");
    private static final LongQualifier LISTEN_COUNT = new LongQualifier("listenCount");
    private static final IntQualifier AGE = new IntQualifier("age");
    private static final StringQualifier COUNTRY = new StringQualifier("country");
    private static final StringQualifier GENDER = new StringQualifier("gender");
    private static final IntQualifier PLAYCOUNT = new IntQualifier("playcount");
    private static final StringQualifier REALNAME = new StringQualifier("realname");
    private static final StringQualifier URL = new StringQualifier("url");
    public static final StringQualifier USERNAME = new StringQualifier("username");

    @Override
    public Family[] getAllFamilies() {
        return new Family[]{DATA, STATISTICS};
    }

    public static void addAge(Put put, int age) {
        addData(put, AGE, age);
    }

    public static void addPlaycount(Put put, int age) {
        addData(put, PLAYCOUNT, age);
    }

    public static void addCountry(Put put, String age) {
        addData(put, COUNTRY, age);
    }

    public static void addGender(Put put, String age) {
        addData(put, GENDER, age);
    }

    public static void addRealname(Put put, String age) {
        addData(put, REALNAME, age);
    }

    public static void addURL(Put put, String age) {
        addData(put, URL, age);
    }

    public static void addUsername(Put put, String age) {
        addData(put, USERNAME, age);
    }

    private static void addData(Put put, StringQualifier qualifier, String value) {
        TableStructure.addData(put, DATA, qualifier, value);
    }

    private static void addData(Put put, IntQualifier qualifier, int value) {
        TableStructure.addData(put, DATA, qualifier, value);
    }

    public static void addFirstVoting(Put put) {
        TableStructure.addData(put, STATISTICS, VOTE_COUNT, 1L);
    }

    public static void incrementVoting(HTable tableInstance, String artistId) throws IOException {
        TableStructure.incrementColumnValue(tableInstance, artistId, STATISTICS, VOTE_COUNT, 1L);
    }

    public static void incrementListenCount(HTable tableInstance, String artistId, long listenCount) throws IOException {
        TableStructure.incrementColumnValue(tableInstance, artistId, STATISTICS, LISTEN_COUNT, listenCount);
    }

    public static long getVotingCount(HTable tableInstance, String artistId) throws IOException {
        return getVotingCount(tableInstance, Bytes.toBytes(artistId));
    }

    public static long getVotingCount(HTable tableInstance, byte[] artistId) throws IOException {
        return TableStructure.getSingleColumnValue(tableInstance, artistId,
                STATISTICS, VOTE_COUNT);
    }

    public static long getListenCount(HTable tableInstance, String artistId) throws IOException {
        return getListenCount(tableInstance, Bytes.toBytes(artistId));
    }

    public static long getListenCount(HTable tableInstance, byte[] artistId) throws IOException {
        return TableStructure.getSingleColumnValue(tableInstance, artistId,
                STATISTICS, LISTEN_COUNT);
    }
}
